package Presenter;

import java.io.File; 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import Model.Player; 
import Model.SaveLoad.SaveLoadModel;
import Run.Engine;
import View.GUI;
import View.LoadView;
import View.MainPageView;
import View.SaveView;


import javax.swing.JFrame;
import javax.swing.JLabel;
/*
 * Class handles the saving and loading functionality
 */
public class SaveLoadPresenter {
	/**
	 * 
	 */
	public final static String[] FILE_NAMES = {"loadFile", "loadFile1", "loadFile2"};

	/**
	 * 
	 */
	private SaveLoadModel model;

	public SaveLoadPresenter(){
		model = new SaveLoadModel(Engine.planets, Player.getInstance());
	}

	public void saveGame(String fileName) throws IOException{
		ObjectOutputStream os = null;
		try {
			os = new ObjectOutputStream(new FileOutputStream(fileName));
	
			os.writeObject(model);
			
		} catch (FileNotFoundException e) {
			System.out.println("couldn't find your file");
		} 
		catch (IOException e) {
			e.printStackTrace();
			System.out.println("failed/interrupted I/O operation when saving");
		} finally {
            os.close();
        }
	
	}
	
	/*
	 * loads the game
	 * @param fileName
	 */
	public void loadGame(String fileName){
		final ObjectInputStream is;
		try {
			is = new ObjectInputStream(new FileInputStream(fileName));
			try {
				model = (SaveLoadModel) is.readObject();
				Engine.planets = model.getPlanets();
				Player.setPlayer(model.getPlayer());
				
			} catch (ClassNotFoundException e) {
				System.out.println("couldn't find your class to in the save file");
			} finally{
			    is.close();
			}
			
		} catch (FileNotFoundException e1) {
			System.out.println("This is trusted. This exception should never be thrown");
			
		} catch (IOException e1) {
			e1.printStackTrace();
			System.out.println("failed/interrupted I/O operation when loading");
		}
	}
	/*
	 * checking if save file is empty or not.
	 */
	
	public Player getFileData(String fileName) throws FileNotFoundException, IOException, ClassNotFoundException{
		final File file = new File(fileName);
		ObjectInputStream is = null;
		SaveLoadModel out = null;
		if(file.length() != 0){
		    try{
		        is = new ObjectInputStream(new FileInputStream(fileName));
		        out = (SaveLoadModel) is.readObject();
		    }catch(ClassNotFoundException e){
		        e.printStackTrace();
		    } finally {
		        is.close();
		    }
			
			return out.getPlayer();
		}
		else return null;
	}
	
	/*
	 * getter for the view
	 * @return SaveLoadView instance
	 */
	public SaveView getSaveView(){
		return new SaveView();
	}
	
	public LoadView getLoadView(){
		return new LoadView();
	}
	
	public SaveLoadModel getModel(){
		return model;
	}
	
	public String toString(){
		return super.toString();
	}
	
//	public static void main(String[] args){
//		SaveLoadPresenter presenter = new SaveLoadPresenter();
//		try {
//			Player p = (Player)presenter.getFileData(FILE_NAMES[0]);
//			System.out.println(p.getName());
//			
//		} catch (ClassNotFoundException | IOException e) {
//			System.out.println("poop");
//		}
//	}

}
